package com.concerneyes.mine.TabActivity.OrderView;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import com.concerneyes.R;
import com.concerneyes.mine.model.MyAdressModel;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import common.AbsActivity;
import common.PreferUtil;
import http.HttpAsyncTask;
import http.JsonMap;
import http.Url;

/**
 *退租申请
 */

public class QuitRentActivity extends AbsActivity implements View.OnClickListener {
    private ImageView mIvGone;
    private FrameLayout mFlEditInfo;
    /**
     * 请填写
     */
    private EditText mOrderNum;
    /**
     * 请填写退租原因
     */
    private EditText mEtDetail;
    /**
     * 提交申请
     */
    private TextView mSubmit;
    private TextView mTextSize;
    private String orderId;
    private Spinner sp_tk;
    private String itemNum;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_quit_rent);
        initView();
        setTitle(R.string.quit_rent);
    }

    private void initView() {
        orderId = getIntent().getExtras().getString("OrderId");
        mIvGone = (ImageView) findViewById(R.id.iv_gone);
        mFlEditInfo = (FrameLayout) findViewById(R.id.fl_edit_info);
        mOrderNum = (EditText) findViewById(R.id.order_num);
        mEtDetail = (EditText) findViewById(R.id.et_detail);
        mSubmit = (TextView) findViewById(R.id.submit);
        mTextSize = (TextView) findViewById(R.id.text_size);
        sp_tk = (Spinner) findViewById(R.id.sp_tk);
        mSubmit.setOnClickListener(this);
        mIvGone.setOnClickListener(this);
        mEtDetail.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                mTextSize.setText(s.length() + "/100");
            }
        });
        String[] logArray = this.getResources().getStringArray(R.array.log_company);
        List<String> logList = new ArrayList<>();
        for (int i = 0; i < logArray.length; i++) {
            logList.add(logArray[i]);
        }
        initSp(logList, sp_tk);
    }

    private void initSp(List<String> array, Spinner spinner) {
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.view_spinner_yupei, R.id.txtvwSpinner,
                array);
        adapter.setDropDownViewResource(R.layout.view_dropdown_spinner_hz);
        spinner.setAdapter(adapter);
        itemNum = array.get(0);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            // parent： 为控件Spinner view：显示文字的TextView position：下拉选项的位置从0开始
            @SuppressWarnings("unchecked")
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                TextView tvResult = (TextView) findViewById(R.id.txtvwSpinner);
                // 获取Spinner控件的适配器
                ArrayAdapter<String> adapter = (ArrayAdapter<String>) parent.getAdapter();
                itemNum = adapter.getItem(position);
            }

            // 没有选中时的处理
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_gone:
                mFlEditInfo.setVisibility(View.GONE);
                break;
            case R.id.submit:
                String orderNum = mOrderNum.getText().toString();
                String etDetail = mEtDetail.getText().toString();
                if (TextUtils.isEmpty(orderNum)) {
                    showToast(R.string.log_num);
                    return;
                }
                if (TextUtils.isEmpty(etDetail)) {
                    showToast(R.string.quit_rent_edit);
                    return;
                }
                applyBack(itemNum, orderNum, etDetail);
                break;
        }
    }

    public void applyBack(String expressCom, String expressNo, String cancelRemark) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("uid", String.valueOf(PreferUtil.getUserId()));
        params.put("id", orderId);
        params.put("expressCom", expressCom);
        params.put("expressNo", expressNo);
        params.put("cancelRemark", cancelRemark);
        HttpAsyncTask task = new HttpAsyncTask() {
            @Override
            public void onStart(String arg0) {
                showLoadingDialog(R.string.loading);
            }

            @Override
            public void onFinish(String arg0, String arg1) {
                cancelLoadingDialog();
                JsonMap ret = JsonMap.parseJson(arg1);
                if (TextUtils.isEmpty(arg1)) {
                    showToast(R.string.network_error_tip);
                    return;
                }
                if (ret.getInt("code") == 200) {
                    Intent intent = new Intent();
                    setResult(202, intent);
                    finish();
                } else {
                    showToast(ret.getString("msg"));
                }
            }
        };
        task.setDebug(true);
        task.excute(Url.BaseUrl + "/order/applyBack", params, "POST");
    }

}
